package org.anyin.gitee.cloud.center.upms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.anyin.gitee.cloud.center.upms.controller.form.SysUserPageForm;
import org.anyin.gitee.cloud.center.upms.controller.form.SysUserSaveForm;
import org.anyin.gitee.cloud.center.upms.convert.SysUserConvert;
import org.anyin.gitee.cloud.center.upms.model.ext.SysUserExt;
import org.anyin.gitee.cloud.center.upms.service.SysUserService;
import org.anyin.gitee.cloud.center.upms.service.dto.SysUserInfoDTO;
import org.anyin.gitee.cloud.center.upms.service.dto.SysUserPageDTO;
import org.anyin.gitee.cloud.center.upms.service.dto.SysUserSaveDTO;
import org.anyin.gitee.cloud.lib.core.web.ApiResponse;
import org.anyin.gitee.cloud.lib.core.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/sys-user")
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserConvert sysUserConvert;

    @PostMapping("/create")
    public ApiResponse create(@Valid  @RequestBody SysUserSaveForm form){
        SysUserSaveDTO info = sysUserConvert.getSysUserSaveDTO(form);
        sysUserService.create(info);
        return ApiResponse.success();
    }

    @GetMapping("/info/{id}")
    public ApiResponse<SysUserInfoDTO> info(@PathVariable("id") Long id){
        SysUserInfoDTO info = sysUserService.info(id);
        return ApiResponse.success(info);
    }

    @PostMapping("/modify/{id}")
    public ApiResponse modify(@PathVariable("id") Long id, @Valid  @RequestBody SysUserSaveForm form){
        SysUserSaveDTO info = sysUserConvert.getSysUserSaveDTO(form);
        sysUserService.modify(id, info);
        return ApiResponse.success();
    }

    @PostMapping("/delete/{id}")
    public ApiResponse delete(@PathVariable("id") Long id){
        sysUserService.delete(id);
        return ApiResponse.success();
    }

    @PostMapping("/page")
    public ApiResponse<Page<SysUserExt>> page(@RequestBody SysUserPageForm form){
        SysUserPageDTO query = sysUserConvert.getSysUserPageDTO(form);
        Page<SysUserExt> page = sysUserService.page(query);
        return ApiResponse.success(page);
    }
}
